﻿(function($j) {
    $j.addFunction = function(c, s) {
        if (c.gridformat) return false;
        p = $j.extend({
            minwidth: 10,
            colresize: true,
            headerTdWidth: 150,
            fromTable: false,
            pagerAble: false,
            showFooter: false
        }, s);
        /* g class defind */
        var g = {
            initG: function(c) {
                g.parent = c;
                if (p.fromTable) {
                    $j('table', c).hide();
                    var _rows_scroll = document.createElement('div');
                    _rows_scroll.className = "default_g_rows_scroll";
                    var _rows_container = document.createElement('div');
                    _rows_container.className = "default_g_rows_container";
                    $j('tr', c).each(function(n) {
                        if (n == 0) {
                            var _header = document.createElement('div');
                            _header.className = "default_g_header";
                            var _header_container = document.createElement('div');
                            _header_container.className = "default_g_header_container";
                            var _header_table = document.createElement('table');
                            $j(_header_table).append($j(this));
                            _header_table.className = "default_g_header_table";
                            $j(_header_container).append(_header_table);
                            $j(_header).append(_header_container);
                            $j(c).append(_header);
                        }
                        else {
                            var _rows = document.createElement('div');
                            var _rows_table = document.createElement('table');
                            if (0 == n % 2) {
                                $j(_rows).addClass("default_g_a_rows");
                                $j(_rows_table).addClass("default_g_a_rows_table");
                            }
                            else {
                                $j(_rows).addClass("default_g_rows");
                                $j(_rows_table).addClass("default_g_rows_table");
                            }
                            $j(_rows_table).append($j(this));
                            $j(_rows).append(_rows_table);
                            $j(_rows_container).append(_rows);
                        }
                        $j(_rows_scroll).append(_rows_container);
                    });
                    $j(c).append(_rows_scroll);
                }
            },
            initInput: function(width, obj, index) {
                if ($j('.input_g', obj)[0]) {
                    //$j(obj).css({ paddingLeft: '0', paddingRight: '0' });
                    //($j("th[axis='" + index + "']", g.headerTable) || $j("td[axis='" + index + "']", g.headerTable)).css({ paddingLeft: '0', paddingRight: '0' });
                    var _p = $j.unitT($j(obj).css('borderLeftWidth')) + $j.unitT($j(obj).css('borderRightWidth'));
                    var _b = $.browser.msie ? $j.unitT($j('.input_g', obj).css('borderLeftWidth')) + $j.unitT($j('.input_g', obj).css('borderRightWidth')) : 0;
                    var _t = ($j('.field_package', obj).size() > 0) ? 17 : 0;
                    var _nw = width - _p - _b - _t - 1;
                    //$j('.input_g', obj).css({ border: '0' });
                    //$j(obj).css({backgroundColor:'#fff'});
                    $j('.input_g', obj).width(_nw);
                }
            },
            getHeaderTdW: function(td) {
                var _hw = $j(td).attr('width');
                if ('' == _hw || 'auto' == _hw || null == _hw) {
                    _hw = p.headerTdWidth;
                }
                return $j.unitT(_hw);
            },
            initRowsTd: function() {
                var ie7 = ($.browser.msie && ($.browser.version == '7.0' || $.browser.version == '6.0'));
                var _hwa = new Array();
                ($j('th', g.headerTable) || $j('td', g.headerTable)).each(function(j) {
                    var _hw = g.getHeaderTdW(this);
                    if (ie7) {
                        $j(this).removeAttr('style');
                    }
                    $j("td[axis='" + j + "']", g.rowTable).each(function() { $j(this).css('width', _hw); g.initInput(_hw, this, j); });
                    $j("td[axis='" + j + "']", g.rowATable).each(function() { $j(this).css('width', _hw); g.initInput(_hw, this, j); });
                    if (p.showFooter)
                        $j("td[axis='" + j + "']", g.footerTable).css('width', _hw);
                    _hwa[j] = _hw;
                });
                $j(g.headerTable).data('hwa', _hwa);
            },
            ajaxInitRowsTd: function() {
                var _hwa = $j(g.headerTable).data('hwa');
                for (var i = 0; i < _hwa.length; i++) {
                    $j("td[axis='" + i + "']", g.rowTable).each(function() { $j(this).css('width', _hwa[i]); g.initInput(_hwa[i], this, i); });
                    $j("td[axis='" + i + "']", g.rowATable).each(function() { $j(this).css('width', _hwa[i]); g.initInput(_hwa[i], this, i); });
                    /*if (p.showFooter) {
                    $j("td[axis='" + i + "']", g.footerTable).css('width', _hwa[i]);
                    }*/
                }
            },
            ajaxInitRowsFooterTd: function() {
                var _hwa = $j(g.headerTable).data('hwa');
                for (var i = 0; i < _hwa.length; i++) {
                    $j("td[axis='" + i + "']", g.footerTable).css('width', _hwa[i]);
                }
            },
            rePosDragHandle: function() {
                ($j('th', g.headerTable) || $j('td', g.headerTable)).each(function(n) {
                    var pos = $j(this).offset().left - $j(g.headerContainer).offset().left + $j(this).outerWidth();
                    var _dw = $j.unitT($('div:eq(' + n + ')', g.cDragHandle).width());
                    $('div:eq(' + n + ')', g.cDragHandle).css({ left: (pos - _dw / 2) });
                });
            },

            rePosDrag: function() {
                var _t = $j(g.parent).offset().top;
                $j(g.cDragLineLeft).css({ top: _t });
                $j(g.cDragLineRight).css({ top: _t });

            },
            initWidth: function() {
                var newW = 0;
                var _ind = 0;
                /*
                ($j('th', g.headerTable) || $j('td', g.headerTable)).each(function(n) {
                //var _w = $j.unitT($j(this).attr('width'));
                var _w = $j(this).width(); //g.getHeaderTdW(this);
                var _p = $j.unitT($j(this).css('paddingLeft')) + $j.unitT($j(this).css('paddingRight'));
                var _b = $j.unitT($j(this).css('borderLeftWidth')) + $j.unitT($j(this).css('borderRightWidth'));
                newW += _w + _p + _b;
                });
                */
                var _hwa = $j(g.headerTable).data('hwa');
                for (var i = 0; i < _hwa.length; i++) {
                    newW += _hwa[i];
                }
                g.headerTable.width(newW);
                if (p.showFooter)
                    g.footerTable.width(newW);
                g.fixWidth();

            },
            fixWidth: function() {
                var newW = $j.unitT(this.headerTable.width());
                this.rowATable.width(newW);
                this.rowTable.width(newW);
                this.rowsContainer.width(newW);
                this.headerContainer.width(newW);
                if (p.showFooter)
                    this.footerContainer.width(newW);
            },
            fixDragHandleHeight: function() {
                var newH = g.headerTable.clientHeight;
                $j('div', this.cDragHandle).each(
						function() {
						    $(this).height(newH);
						}
					);
            },
            fixDragHeight: function() {
                var _dragH = g.parent.height();
                if (p.pagerAble)
                    _dragH = _dragH - g.pager.outerHeight(true);
                $j(g.cDragLineLeft).height(_dragH);
                $j(g.cDragLineRight).height(_dragH);
            },
            scroll: function() {
                var _scrollL = this.rowsScrollDiv.scrollLeft();
                this.headerDiv.scrollLeft(_scrollL);
                if (p.showFooter) {
                    this.footerDiv.scrollLeft(_scrollL);
                }
            },
            dragStart: function(e, dragType, dragHandle) {
                g.rePosDrag();
                g.fixDragHeight();
                if (dragType == 'colresize') {
                    var _index = $j(dragHandle).attr('axis');
                    if ('0' != _index) {
                        $j(g.cDragLineLeft).css({ left: e.pageX - ($j("th[axis='" + _index + "']", g.headerTable) || $j("td[axis='" + _index + "']", g.headerTable)).outerWidth() }).show();
                    }
                    $j(g.cDragLineRight).css({ left: e.pageX }).show();

                    var ow = ($j("th[axis='" + _index + "']", g.headerTable) || $j("td[axis='" + _index + "']", g.headerTable)).width();
                    var ohw = this.headerTable.width();
                    this.colresize = { startX: e.pageX, ol: parseInt(dragHandle.style.left), ow: ow, n: _index, ohw: ohw };
                    $j('body').css('cursor', 'col-resize');
                    $j('body').noSelect(true);
                }
            },
            dragMove: function(e) {
                if (this.colresize) {
                    var n = this.colresize.n;
                    var diff = e.pageX - this.colresize.startX;
                    var nleft = this.colresize.ol + diff;
                    var nw = this.colresize.ow + diff;
                    if (nw > p.minwidth) {
                        $j(g.cDragLineRight).css('left', e.pageX);
                        this.colresize.nw = nw;
                    }
                }
            },
            dragEnd: function() {
                if (this.colresize) {
                    var n = this.colresize.n;
                    var ow = this.colresize.ow;
                    if (undefined != this.colresize.nw) {
                        var nw = this.colresize.nw;
                        ($j("th:visible[axis='" + n + "']", g.headerTable) || $j("td:visible[axis='" + n + "']", g.headerTable)).css('width', nw);
                        $j("td:visible[axis='" + n + "']", g.rowTable).each(function() { $j(this).css('width', nw); g.initInput(nw, this); });
                        $j("td:visible[axis='" + n + "']", g.rowATable).each(function() { $j(this).css('width', nw); g.initInput(nw, this); });
                        var _nw = this.colresize.ohw + nw - ow;
                        if (p.showFooter) {
                            $j("td:visible[axis='" + n + "']", g.footerTable).css('width', nw);
                            this.footerTable.width(_nw)
                        }

                        this.headerTable.width(_nw);
                        g.fixWidth();

                        this.headerDiv.scrollLeft(this.rowsScrollDiv.scrollLeft());
                        g.rePosDragHandle();
                        var _hwa = $j(g.headerTable).data('hwa');
                        _hwa[n] = nw;
                        $j(g.headerTable).data('hwa', _hwa);
                    }
                    $(g.cDragLineLeft).hide();
                    $(g.cDragLineRight).hide();
                    this.colresize = false;
                }

                $('body').css('cursor', 'default');
                $j('body').noSelect(false);
            },
            rowMouseEvent: function() {
                g.rowTable
                .mouseover(function() { $j(this).addClass('default_row_over'); })
                .mouseout(function() { $j(this).removeClass('default_row_over'); })
                ;
                g.rowATable
                .mouseover(function() { $j(this).addClass('default_row_over'); })
                .mouseout(function() { $j(this).removeClass('default_row_over'); })
                ;
            }
        };
        /* g class defind  end*/

        /*init g*/
        g.initG(c);
        g.headerDiv = $j('.default_g_header', c);
        g.rowsScrollDiv = $j('.default_g_rows_scroll', c);
        g.headerTable = $j('.default_g_header_table', c);
        g.rowsContainer = $j('.default_g_rows_container', c);
        g.headerContainer = $j('.default_g_header_container', c);
        g.rowDiv = $j('.default_g_rows', c);
        g.rowADiv = $j('.default_g_a_rows', c);
        g.gridDiv = $j('.default_g', c);
        g.rowATable = $j('.default_g_a_rows_table', c);
        g.rowTable = $j('.default_g_rows_table', c);
        if (p.pagerAble)
            g.pager = $j('.default_g_pager', c);
        if (p.showFooter) {
            g.footerDiv = $j('.default_g_footer', c);
            g.footerTable = $j('.default_g_footer_table', c);
            g.footerContainer = $j('.default_g_footer_container', c);
        }
        g.rowsScrollDiv.block({ message: "<img id=\'_" + g.rowsContainer.attr("id") + "loadingimg__\' class=\'loadingimg\' src=\'" + $Page.ResourcesURL + "Images/loading.gif\' />",
            overlayCSS: { backgroundColor: '#FAFFF0' }, css: { border: 0, backgroundColor: 'transparent' }
        });
        /*set g width*/

        /*scroll*/
        $j(g.rowsScrollDiv).scroll(function(e) { g.scroll(); });

        /*cell drag handle div*/
        g.cDragHandle = document.createElement('div');
        g.cDragHandle.className = 'default_cDrag_h';
        $j(g.cDragHandle).css({ top: 0 - $j(g.headerContainer).height() });
        ($j('th', g.headerTable) || $j('td', g.headerTable)).each(function(n) {
            $j(this).attr('axis', n);
            var cgDiv = document.createElement('div');
            $j(cgDiv).attr('axis', n)
                .mousedown(function(e) {
                    g.dragStart(e, 'colresize', this);
                })
                .mouseup(function(e) {
                    g.dragEnd();
                });
            $j(g.cDragHandle).append(cgDiv);
        });
        $j(g.headerContainer).append(g.cDragHandle);
        g.initRowsTd();
        g.initWidth();
        g.rePosDragHandle();

        //ajax
        g.rowsContainer.change(function() {
            $j.inputInit();
            g.rowDiv = $j('.default_g_rows', c);
            g.rowADiv = $j('.default_g_a_rows', c);
            g.gridDiv = $j('.default_g', c);
            g.rowATable = $j('.default_g_a_rows_table', c);
            g.rowTable = $j('.default_g_rows_table', c);
            //            if (p.showFooter) {
            //                g.footerTable = $j('.default_g_footer_table', c);
            //            }
            g.ajaxInitRowsTd();
            //g.initWidth();
            g.fixWidth();
            g.rowMouseEvent();
            g.rePosDragHandle();
        });
        if (p.showFooter) {
            g.footerContainer.change(function() {
                g.footerTable = $j('.default_g_footer_table', c);
                g.ajaxInitRowsFooterTd();
            });
        }

        var _cDragLeft = document.createElement('div');
        var _cDragRight = document.createElement('div');
        g.cDragLineLeft = _cDragLeft;
        g.cDragLineRight = _cDragRight;
        g.cDragLineLeft.className = 'default_cDrag_line';
        g.cDragLineRight.className = 'default_cDrag_line';
        $j(g.parent).append(g.cDragLineLeft);
        $j(g.parent).append(g.cDragLineRight);

        g.fixDragHeight();

        $(document)
		        .mousemove(function(e) { g.dragMove(e) })
		        .mouseup(function(e) { g.dragEnd() })
		        .hover(function() { }, function() { g.dragEnd() })
		        ;
        g.rowMouseEvent();

        c.gridformat = g;
        g.rowsScrollDiv.unblock();
        return c;
    };

    $j.fn.grid = function(s) {
        $j.addFunction(this, s);
    };



    //unit transaction
    $j.unitT = function(u) {
        var _u = u.toString();
        if (null == _u || '' == _u)
            return 0;
        return parseInt(_u.replace('px', ''));
    }
    $j.fn.noSelect = function(p) { //no select plugin 
        if (p == null)
            prevent = true;
        else
            prevent = p;
        if (prevent) {
            return this.each(function() {
                if ($.browser.msie || $.browser.safari) $(this).bind('selectstart', function() { return false; });
                else if ($.browser.mozilla) {
                    $(this).css('MozUserSelect', 'none');
                    $('body').trigger('focus');
                }
                else if ($.browser.opera) $(this).bind('mousedown', function() { return false; });
                else $(this).attr('unselectable', 'on');
            });
        } else {
            return this.each(function() {
                if ($.browser.msie || $.browser.safari) $(this).unbind('selectstart');
                else if ($.browser.mozilla) $(this).css('MozUserSelect', 'inherit');
                else if ($.browser.opera) $(this).unbind('mousedown');
                else $(this).removeAttr('unselectable', 'on');
            });
        }
    } //end noSelect
    $j.nested = function(obj, url, nestedType, hei) {
        if (nestedType == 'newwindow') {
            window.open(url, '');
        }
        else if (nestedType == 'iframe' || nestedType == 'ajax' || nestedType == 'popup') {
            var _id = $j(obj).attr("id");
            var _nested_panel_id = _id + "_nested_panel";
            if ($j("#" + _nested_panel_id).length > 0) {
                var dis = $j('#' + _nested_panel_id).css('display');
                if ('none' != dis) {
                    $j(obj).attr('class', 'default_nested_icon default_nested_plus');
                    $j('#' + _nested_panel_id).hide();
                }
                else {
                    $j(obj).attr('class', 'default_nested_icon default_nested_minus');
                    $j('#' + _nested_panel_id).show();
                }
            }
            else {
                var _new_element;
                /*iframe*/
                if (nestedType == 'iframe' || nestedType == 'popup') {
                    _new_element = "<div id=\""
            + _nested_panel_id
            + "\" class=\"default_nested_panel" + (nestedType == 'popup' ? '_pop' : '') + "\"><iframe  frameBorder=\"0\" marginWidth=\"0\" marginHeight=\"0\" scrolling=\"auto\" class=\"default_nested_iframe\" src=\""
            + url
            + "\"></iframe></div>";
                }
                else if (nestedType == 'ajax') {
                    _new_element = "<div id=\""
            + _nested_panel_id
            + "\" class=\"default_nested_panel\"></div>";
                }

                var row_table = $j(obj).parent().parent().parent().parent();
                $j(row_table).after(_new_element);

                $j(obj).attr("class", "default_nested_icon default_nested_minus");


                if (hei != undefined && hei != null && hei != "") {
                    $j("#" + _nested_panel_id).height(hei);
                }

                /*ajax*/
                if (nestedType == 'ajax') {
                    $.ajax({
                        url: url,
                        dataType: 'html',
                        success: function(html) {
                            $("#" + _nested_panel_id).append(html);
                        }
                    });
                }
                if (nestedType == 'popup') {
                    var offset = $j(row_table).position();
                    var hei = $j(row_table).height();
                    var wid = $j(row_table).width();
                    $j("#" + _nested_panel_id).css({ left: offset.left, top: offset.top + hei + 1, position: 'absolute', width: wid });
                }
            }
        }
    };
    //end nested

})(jQuery);




